<?php
class expand_modelModel extends baseModel{
    protected $table = 'expand_model';

    //列表
    public function model_list() {
        return $this->select('','','mid asc');
    }

    //获取模型信息
    public function table_info($table,$mid=null) {
        $where="`table`='".$table."'";
        if(!empty($mid)){
        $where.=' AND mid<>'.$mid;
        }
        return $this->find($where);
    }

    //获取模型信息
    public function info($mid) {
        return $this->find('mid='.$mid);
    }

    //添加
    public function add($data) {
        //建立模型添加表
        $sql="
        CREATE TABLE IF NOT EXISTS `{$this->model->pre}expand_content_{$data['table']}` (
          `id` int(10) NOT NULL AUTO_INCREMENT,
          `aid` int(10) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
        ";
        $this->query($sql);
        return $this->insert($data);
    }

    //修改
    public function edit($data) {
        $info=$this->info($data['mid']);
        //修改模型表
        $sql="
        ALTER TABLE {$this->model->pre}expand_content_{$info['table']} RENAME TO {$this->model->pre}expand_content_{$data['table']}
        ";
        $this->model->query($sql);
        //修改信息
        $condition['mid']=intval($data['mid']);
        return $this->update($condition,$data); 
    }

    //删除
    public function del($data)
    {
        $info=$this->info($data['mid']);
        //删除表
        $sql="
        DROP TABLE `{$this->model->pre}expand_content_{$info['table']}`
        ";
        $this->model->query($sql);
        //删除表内字段
        model('expand_model_field')->model_del($data['mid']);
        return $this->delete('mid='.$data['mid']); 
    }

}

?>